﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.FireProjects.BLL;
using Yipusoft.FireProjects.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class BasicBase_FireProject_FireProjectMainStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvStat.DataSource = Yipusoft.Account.BLL.Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000')");
            gvStat.DataBind();
            for (int i = 2000; i <= 2050; i++)
            {
                ddlYear.Items.Add(new ListItem(i.ToString()));
            }
            for (int i = 1; i <= 12; i++)
            {
                ddlMonth.Items.Add(new ListItem(i.ToString()));
            }

            ddlYear.SelectedValue = DateTime.Now.Year.ToString();
            ddlMonth.SelectedValue = DateTime.Now.Month.ToString();
            Qurey();

            gvStat.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            gvStat.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Left;
            gvStat.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Left;
            gvStat.FooterRow.Cells[4].HorizontalAlign = HorizontalAlign.Left;
            gvStat.FooterRow.Cells[3].HorizontalAlign = HorizontalAlign.Left;
        }
    }
    protected void gvStat_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection tcl = e.Row.Cells;
            //清除自动生成的表头
            tcl.Clear();

            //添加新的表头
            tcl.Add(new TableHeaderCell());
            tcl[0].Text = "单位";
            tcl[0].Attributes.Add("style", "border-color:#CEE7F7;");


            tcl.Add(new TableHeaderCell());
            tcl[1].Text = "当前总个数";

            tcl[1].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());
            tcl[2].Text = "当月总个数";

            tcl[2].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());
            tcl[3].Text = "添加个数";

            tcl[3].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());

            tcl[4].Text = "更新个数";

            tcl[4].Attributes.Add("style", "border-color:#CEE7F7;");


        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            TableCellCollection tcl = e.Row.Cells;
            //清除自动生成的表头
            tcl.Clear();

            //添加新的表头
            tcl.Add(new TableHeaderCell());
            tcl[0].Text = "总数";
            tcl[0].Attributes.Add("style", "border-color:#CEE7F7;");


            tcl.Add(new TableHeaderCell());


            tcl[1].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());


            tcl[2].Attributes.Add("style", "border-color:#CEE7F7;");
            tcl.Add(new TableHeaderCell());



            tcl[3].Attributes.Add("style", "border-color:#CEE7F7;");

            tcl.Add(new TableHeaderCell());



            tcl[4].Attributes.Add("style", "border-color:#CEE7F7;");

        }
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        Qurey();
    }
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        Qurey();
    }

    void Qurey()
    {
        int all = 0;
        int mothall = 0;
        int add = 0;
        int update = 0;
        foreach (GridViewRow row in gvStat.Rows)
        {
            Label lblDepartmentid = row.FindControl("lblDepartmentid") as Label;
            string departmentid = " departmentid like '" + lblDepartmentid.Text.Substring(0, 4) + "3%'  ";
            DateTime startDate = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), 1);
            DateTime endDate = startDate.AddMonths(1);
            //当前
            string AllWhere = departmentid + "";
            HyperLink HyAll = row.FindControl("HyAll") as HyperLink;
            HyAll.Text = FireProject.GetCount(AllWhere).ToString();
            HyAll.NavigateUrl = "FireProjectList.aspx?where=" + AllWhere;
            //当月
            string AllMothWhere = departmentid + " and SetTime<'" + endDate.ToString() + "'";
            HyperLink HyMothAll = row.FindControl("HyMothAll") as HyperLink;
            HyMothAll.Text = FireProject.GetCount(AllMothWhere).ToString();
            HyMothAll.NavigateUrl = "FireProjectList.aspx?where=" + AllMothWhere;

            HyperLink HyAdd = row.FindControl("HyAdd") as HyperLink;
            HyperLink HyUpdate = row.FindControl("HyUpdate") as HyperLink;
            //添加个数
            string Addwhere = departmentid + " and setTime>='" + startDate.ToString() + "' and  setTime<'" + endDate.ToString() + "'";
            HyAdd.Text = FireProject.GetCount(Addwhere).ToString();
            HyAdd.NavigateUrl = "FireProjectList.aspx?where=" + Addwhere;

            //更新个数
            string Updatewhere = departmentid + " and UpdateTime<>setTime and UpdateTime>='" + startDate.ToString() + "' and  UpdateTime<'" + endDate.ToString() + "' ";
            HyUpdate.Text = FireProject.GetCount(Updatewhere).ToString();
            HyUpdate.NavigateUrl = "FireProjectList.aspx?where=" + Updatewhere;

            all += Convert.ToInt32(HyAll.Text);
            mothall += Convert.ToInt32(HyMothAll.Text);
            add += Convert.ToInt32(HyAdd.Text);
            update += Convert.ToInt32(HyUpdate.Text);
        }


        gvStat.FooterRow.Cells[1].Text = all.ToString();

        gvStat.FooterRow.Cells[2].Text = mothall.ToString();

        gvStat.FooterRow.Cells[3].Text = add.ToString();

        gvStat.FooterRow.Cells[4].Text = update.ToString();
    }
}
